package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMtyq;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMtyqParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMtyqResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyMtyqService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2021-07-29 10:44:11
 */
@Controller
@RequestMapping("/btyMtyq")
public class BtyMtyqController extends BaseController {

    private String PREFIX = "/modular/system/btyMtyq";

    @Autowired
    private BtyMtyqService btyMtyqService;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyMtyq.html";
    }

    @RequestMapping("/upload")
    public String upload() {
        return PREFIX + "/uploadTxt.html";
    }

    /**
     * 查询店名
     */
    @RequestMapping(value = "/dmList")
    @ResponseBody
    public List<BtyMtyqResult> dmList() {
        String account = LoginContextHolder.getContext().getUser().getName();
        Long userId= LoginContextHolder.getContext().getUserId();
        BtyMtyqParam btyMtyqParam=new BtyMtyqParam();
        List<BtyMtyqResult> list;
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        if (account.equals("admin")) {
            list = btyMtyqService.dmList(btyMtyqParam);
        } else {
            btyMtyqParam.setCreateUser(userId);
            list = btyMtyqService.dmList(btyMtyqParam);
        }
        return list;
    }

    /**
     * 导入txt
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        String fileName = file.getOriginalFilename();
        try {
            flag = btyMtyqService.batchImportTxt(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;

    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyMtyq_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyMtyq_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyMtyqParam btyMtyqParam) {
        this.btyMtyqService.add(btyMtyqParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyMtyqParam btyMtyqParam) {
        this.btyMtyqService.update(btyMtyqParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyMtyqParam btyMtyqParam) {
        this.btyMtyqService.delete(btyMtyqParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyMtyqParam btyMtyqParam) {
        BtyMtyq detail = this.btyMtyqService.getById(btyMtyqParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2021-07-29
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyMtyqParam btyMtyqParam) {
        return this.btyMtyqService.findPageBySpec(btyMtyqParam);
    }

}


